//
//  WXLStatusOriginalView.swift
//  WXLWeoBo
//
//  Created by 王晓雷 on 16/6/28.
//  Copyright © 2016年 王晓雷. All rights reserved.
//

import UIKit

private let OriginalMargin: CGFloat = 10

class WXLStatusOriginalView: UIView {
    //供外界设置数据
    var status: WXLStatus? {
        didSet{
            //用户昵称
            nameLabel.text = status?.user?.name
            //用户头像
            
            headImageView.sd_setImageWithURL(NSURL(string: (status?.user?.profile_iamge_url) ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
            //时间和来源后期处理
            //微博内容
            contentLabel.text = status?.text
            //认证图片
            vipImageView.image = status?.user?.vertfiedImage
            //会员等级
            expiredImageView.image = status?.user?.mbrankImage
        }
    }
    
       override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        // 01- 添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(expiredImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipImageView)
        addSubview(contentLabel)
        // 02 -添加约束
        // 头像
        headImageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(OriginalMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        // 昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(OriginalMargin)
        }
        // 用户等级
        expiredImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(OriginalMargin)
            make.centerY.equalTo(nameLabel)
        }
        // 发布时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        // 来自
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(OriginalMargin)
        }
        // 认证
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        //微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(OriginalMargin)

        }
        //原创微博
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentLabel).offset(OriginalMargin)
        }
        
        // 临时放到这里
        nameLabel.text = "昵称"
        timeLabel.text = "时间"
        sourceLabel.text = "来自传智播客29期"
        contentLabel.text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        
    }
    
    // MARK: - 懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default_big")
    // 用户昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15)
    // 用户等级common_icon_membership_expired
    private lazy var expiredImageView: UIImageView = UIImageView(imgName: "common_icon_membership_expired")
    // 发布微博事件
    private lazy var timeLabel: UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.grayColor(), textFont: 10)
    // VIP
    private lazy var vipImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    //微博内容
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15,maxWidth: SCREENW - 2 * OriginalMargin)
    
}

extension WXLStatusOriginalView {
    //会员等级 0 -6
    // var mbrank:Int = 0
    func getMbrankImage(mbrank: Int) -> UIImage? {
        //如果会员等级0-6  使用
        if mbrank > 0 && mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }else {
            //其他使用默认
            return UIImage(named: "common_icon_membership_expired")
        }
    }
    
    //通过后台返回的数据判断 认证类型  -1 没有认证  0 认证用户  2,3,5 企业认证  220 :达人
    func getVipImage(verified: Int) -> UIImage? {
        switch verified{
        case 0:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }

}

